package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname ChannelTranslationPluginImpl
 * @Description 渠道商平移
 * @Date 2020/3/29 16:26
 * @Created by fpc
 */
public class ChannelTranslationPluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String oldOrgId = String.valueOf(map.get("uctUser.w_orgId"));//当前所属的渠道商ID(old)
        String newOrgId = String.valueOf(map.get("uctUser.orgId"));//需要修改成为的渠道商ID(new)
        String orgEname = String.valueOf(map.get("uctUser.orgEname"));//需要修改成为的渠道商ID(new)
        List<DataMap> cLlist = selectAllChildLevel(oldOrgId);//查询出所有的子级渠道商ID（包含自己）
        String  cLUserIds="";
        String  userCnames="";
        for (DataMap dataMap:cLlist) {
            if(!oldOrgId.equals(dataMap.get("userId"))){
                cLUserIds += cLUserIds == "" ? dataMap.get("userId") : "," + dataMap.get("userId");
                userCnames += userCnames == "" ? dataMap.get("userCname") : "," + dataMap.get("userCname");
            }
        }
        log.info("CLUserId--->>>"+cLUserIds);
        //删除所有自己渠道商和产品的关系
        Map delMap = new HashMap();
        delMap.put("ssOrgProRelation.wi_userId",cLUserIds);
        IusPluginUtil.getInstance().saveInvoke("ssb","d_ssOrgProRelation",delMap);
        //查询要被平移到的渠道商下有哪些产品
        Map selectProMap = new HashMap();
        selectProMap.put("ssOrgProRelation.w_userId",newOrgId);
        ResultEntity selectProEntity = IusPluginUtil.getInstance().select("ssb", "list", "s_ssOrgProRelation", selectProMap);
        String proIds = "";
        if(!Validator.isEmpty(selectProEntity.getEntity())){
            List<DataMap> selectProList = (List)selectProEntity.getEntity();
            if(selectProList.size()>0){
                for (DataMap dataMap: selectProList) {
                    proIds += proIds==""?dataMap.get("proId"):"-#-"+dataMap.get("proId");
                }
                log.info("proIds----->>>>"+proIds);
                //建立产品和被平移的子级渠道商建立关系，同时默认分佣比例为0
                for (DataMap dataMap:cLlist) {
                    if(!oldOrgId.equals(dataMap.get("userId"))){
                        String userId = String.valueOf(dataMap.get("userId"));
                        String userCname = String.valueOf(dataMap.get("userCname"));
                        Map saveMap = new HashMap();
                        saveMap.put("ssOrgProRelation.userId",userId);
                        saveMap.put("ssOrgProRelation.userOwner",userCname);
                        saveMap.put("ssOrgProRelation.proId",proIds);
                        saveMap.put("ssOrgProRelation.createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                        saveMap.put("ssOrgProRelation.bkgeRatio","0");
                        saveMap.put("ssOrgProRelation.delStatus","F");
                        saveMap.put("ifBatch","T");
                        saveMap.put("ssOrgProRelation.splitAttr","proId");
                        ResultEntity resultEntity = IusPluginUtil.getInstance().saveInvoke("ssb", "ib_ssOrgProRelation", saveMap);
                        log.info(dataMap.get("userId") + "<<<---->>>"+"resultEntity--->>>"+resultEntity);
                    }
                }
            }
        }
        //修改直属下级归属最新渠道商
        Map updateMap = new HashMap();
        updateMap.put("uctUser.w_orgId",oldOrgId);
        updateMap.put("uctUser.orgId",newOrgId);
        updateMap.put("uctUser.orgEname",orgEname);

        IusPluginUtil.getInstance().saveInvoke("plateform", "u_uctUser", updateMap);

        map.put(CustomMethodPlugin.resultMark,"查询成功");
        map.put(CustomMethodPlugin.pluginInnerReturn,"1111");
        return null;
    }

    private List<DataMap> selectAllChildLevel(String oldOrgId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT user_id,user_cname  FROM uct_user WHERE user_type='broker' AND ");
        sb.append("FIND_IN_SET( user_id, queryChannelChildrenInfo ('");
        sb.append(oldOrgId);
        sb.append("'));");
        List<DataMap> cLlist = (List<DataMap>)IusPluginUtil.getInstance().select("plateform",sb.toString(), 0, 0);
        return cLlist;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }

}
